package cn.com.eado.crm.v.fragment


import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.RadioButton
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import cn.com.eado.crm.R
import cn.com.eado.crm.app.extenstion.toast
import cn.com.eado.crm.databinding.FragmentUnearchBinding
import cn.com.eado.crm.model.LoadStatus
import cn.com.eado.crm.model.RetryCallback
import cn.com.eado.crm.v.adapter.RVCompanyAdapter
import cn.com.eado.crm.v.weiget.AutoSpinner
import cn.com.eado.crm.v.weiget.ClearEditText
import cn.com.eado.crm.v.weiget.DateSpinner
import cn.com.eado.crm.vm.CompanyViewModel
import cn.com.eado.preado.app.utils.autoCleared
import com.getbase.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_unearch.*

/**
 * A simple [Fragment] subclass.
 */
class UnearchFragment : BaseBindingFragment<FragmentUnearchBinding>(R.layout.fragment_unearch) {

    private var vm by autoCleared<CompanyViewModel>()
    private var adapter by autoCleared<RVCompanyAdapter>()
    private lateinit var title: String

    override fun initTheFragment() {
        initView()
        initViewModel()
        initBinding()
        loadData()
    }

    private fun initView() {
        initTitleBar()
        initOptionMenu()
        initRecyclerView()
    }

    private fun initTitleBar() {
        title = arguments?.getString("name")?:"XXX管理"
        titleBar3.setTitle(title)
    }

    private fun initOptionMenu(){
        fab_menu2.findViewById<FloatingActionButton>(R.id.fab_1).setOnClickListener {
            val layout = initAddUnearch()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("新增${title.replace("管理","")}")
                .setView(layout)
                .setPositiveButton("提交", null)//结合下面的dialog.getButton，保证点击确定不关闭dialog
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{//必须在show()之后
                if (checkUnearch(layout)) dialog.dismiss()
            }
        }

        fab_menu2.findViewById<FloatingActionButton>(R.id.fab_2).setOnClickListener {
            val layout = initAddTrack()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("添加跟踪")
                .setView(layout)
                .setPositiveButton("提交", null)
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{
                if (checkTrack(layout)) dialog.dismiss()
            }
        }

        fab_menu2.findViewById<FloatingActionButton>(R.id.fab_3).setOnClickListener {
            val layout = initAddNote()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("生成备案")
                .setView(layout)
                .setPositiveButton("提交", null)
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{
                if (checkNote(layout)) dialog.dismiss()
            }
        }

        fab_menu2.findViewById<FloatingActionButton>(R.id.fab_4).setOnClickListener {
            val bundle = Bundle()
            bundle.putString("name", title)
//            Navigation.findNavController(view!!).navigate(R.id.action_unearchFragment_to_searchFragment, bundle)
        }
    }

    private fun initAddUnearch(): View {
        val layout = View.inflate(activity, R.layout.add_unearch, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        val dateSpinner = layout.findViewById<DateSpinner>(R.id.dateSpinner)
        val inputSpinner3 = layout.findViewById<AutoSpinner>(R.id.inputSpinner3)
        val inputSpinner4 = layout.findViewById<AutoSpinner>(R.id.inputSpinner4)
        val inputSpinner5 = layout.findViewById<AutoSpinner>(R.id.inputSpinner5)
        val inputSpinner6 = layout.findViewById<AutoSpinner>(R.id.inputSpinner6)
        val inputSpinner7 = layout.findViewById<AutoSpinner>(R.id.inputSpinner7)
        val inputSpinner8 = layout.findViewById<AutoSpinner>(R.id.inputSpinner8)
        val dateSpinner2 = layout.findViewById<AutoSpinner>(R.id.dateSpinner2)
        val inputSpinner10 = layout.findViewById<AutoSpinner>(R.id.inputSpinner10)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkUnearch(layout: View): Boolean {
        val rbYZ = layout.findViewById<RadioButton>(R.id.rbYZ)
        val rbSJY = layout.findViewById<RadioButton>(R.id.rbSJY)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        val inputSpinner2 = layout.findViewById<AutoSpinner>(R.id.inputSpinner2)
        val inputSpinner3 = layout.findViewById<AutoSpinner>(R.id.inputSpinner3)
        val inputSpinner4 = layout.findViewById<AutoSpinner>(R.id.inputSpinner4)
        val inputSpinner5 = layout.findViewById<AutoSpinner>(R.id.inputSpinner5)
        val inputSpinner6 = layout.findViewById<AutoSpinner>(R.id.inputSpinner6)
        val inputSpinner7 = layout.findViewById<AutoSpinner>(R.id.inputSpinner7)
        val inputSpinner8 = layout.findViewById<AutoSpinner>(R.id.inputSpinner8)
        val dateSpinner2 = layout.findViewById<AutoSpinner>(R.id.dateSpinner2)
        val inputSpinner10 = layout.findViewById<AutoSpinner>(R.id.inputSpinner10)
        val et1 = layout.findViewById<ClearEditText>(R.id.et1)
        val et2 = layout.findViewById<ClearEditText>(R.id.et2)
        val et3 = layout.findViewById<ClearEditText>(R.id.et3)
        val et4 = layout.findViewById<ClearEditText>(R.id.et4)
        val etAddress = layout.findViewById<ClearEditText>(R.id.etAddress)
        //
        if (!rbYZ.isChecked && !rbSJY.isChecked){
            activity?.toast("请选择业主/设计院")
            return false
        }
        if (inputSpinner1.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner1.getName()}")
            return false
        }
        if (inputSpinner2.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner2.getName()}")
            return false
        }
        if (inputSpinner3.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner3.getName()}")
            return false
        }
        if (inputSpinner4.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner4.getName()}")
            return false
        }
        if (inputSpinner5.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner5.getName()}")
            return false
        }
        if (inputSpinner6.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner6.getName()}")
            return false
        }
        if (inputSpinner7.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner7.getName()}")
            return false
        }
        if (inputSpinner8.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner8.getName()}")
            return false
        }
//        if (inputSpinner9.getText().isEmpty()){
//            activity?.toast("请选择${inputSpinner9.getName()}")
//            return false
//        }
        if (inputSpinner10.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner10.getName()}")
            return false
        }
        if (et1.text.trim().isEmpty()){
            activity?.toast("工作内容不能为空")
            return false
        }
        if (et2.text.trim().isEmpty()){
            activity?.toast("解决问题不能为空")
            return false
        }
        if (et3.text.trim().isEmpty()){
            activity?.toast("下步计划不能为空")
            return false
        }
        if (et4.text.trim().isEmpty()){
            activity?.toast("需要支持不能为空")
            return false
        }
        return true
    }

    private fun initAddTrack(): View {
        val layout = View.inflate(activity, R.layout.add_unearch, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkTrack(layout: View): Boolean {
        return true
    }

    private fun initAddNote(): View {
        val layout = View.inflate(activity, R.layout.add_unearch, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkNote(layout: View): Boolean {
        return true
    }


    private fun initRecyclerView() {
        recyclerView3.layoutManager = LinearLayoutManager(activity)
        val adapter = RVCompanyAdapter({ view, pos, bean ->  //click
            //
        },{ pos, bean ->  //long click
            //
        })
        recyclerView3.adapter = adapter
        this.adapter = adapter
    }

    private fun initViewModel(){
        vm = ViewModelProviders.of(this).get(CompanyViewModel::class.java)
//        vm.result.observe(this, Observer {
//            if (it.status == LoadStatus.SUCCESS){
////                Log.e("tag", "it=${it.data}")
//            }
//        })
    }

    private fun initBinding(){
        binding.lifecycleOwner = viewLifecycleOwner
//        binding.result = vm.result
        binding.callback = object : RetryCallback {
            override fun retry() {
                vm.refresh()
            }
        }
    }

    private fun loadData(){
        vm.setLoadCompany(true)
    }


}
